package cn.shyouchi.wuguanjia.android.ui.statistic.page;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class StatisticPageActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rb_month)
    RadioButton mRbMonth;
    @BindView(R.id.rb_year)
    RadioButton mRbYear;
    @BindView(R.id.rg_group)
    RadioGroup mRgGroup;
    @BindView(R.id.page_statistic)
    ViewPager mPageStatistic;

    List<Fragment> mFragments = new ArrayList<>();

    StatisticPageAdapter mPageAdapter;


    String projectId = "";

    AttendanceApi mAttendanceApi;


    @Override
    public int initContentView() {
        return R.layout.activity_statistic_page;
    }

    @Override
    public void initInjector() {

        projectId = String.valueOf(getIntent().getIntExtra("event", 0));
        mAttendanceApi = new AttendanceApi(mContext);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);

        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mFragments.add(new MothStatisticFragment());
        mFragments.add(new YearStatisticFragment());
        mPageAdapter = new StatisticPageAdapter(getSupportFragmentManager());
        mPageStatistic.setAdapter(mPageAdapter);


        mRgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_month:
                        mPageStatistic.setCurrentItem(0);
                        break;
                    case R.id.rb_year:
                        mPageStatistic.setCurrentItem(1);
                        break;

                }
            }
        });

        mPageStatistic.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mRgGroup.check(mRgGroup.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        fetchStatisticDate();


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private class StatisticPageAdapter extends FragmentPagerAdapter {
        public StatisticPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }


    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    public String getProjectId() {
        return projectId;
    }


    private void fetchStatisticDate() {
        mAttendanceApi.getStatisticDate()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<YearStatisticEntity>(mContext) {
                    @Override
                    public void onNext(YearStatisticEntity entity) {
                        EventBus.getDefault().post(entity.getResultMessage());
                    }
                });
    }
}
